﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace Seesmic.HelloTipi
{
    public class RelayCommand : ICommand
    {
        private readonly Func<bool> _canExecute;
        private readonly Action _execute;

        public RelayCommand(string caption, Action execute)
            : this(caption, execute, null)
        {
        }

        public RelayCommand(string caption, Action execute, Func<bool> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }

            this.Caption = caption;
            this._execute = execute;
            this._canExecute = canExecute;
        }

        public override string ToString()
        {
            return this.Caption;
        }

        public string Caption { get; private set; }

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }

        #region ICommand Members

        //[DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return (_canExecute == null) ? true : _canExecute();
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            _execute();
        }

        #endregion
    }

    public class RelayCommand<T> : ICommand
    {
        private readonly Predicate<T> _canExecute;
        private readonly Action<T> _execute;

        public RelayCommand(string caption, Action<T> execute)
            : this(caption, execute, null)
        {
        }

        public RelayCommand(string caption, Action<T> execute, Predicate<T> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }

            this.Caption = caption;
            this._execute = execute;
            this._canExecute = canExecute;
        }

        public override string ToString()
        {
            return this.Caption;
        }

        public string Caption { get; private set; }

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }

        #region ICommand Members

        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return (_canExecute == null) ? true : _canExecute((T)parameter);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            _execute((T)parameter);
        }

        #endregion
    }
}
